
var netUtil = require('/../../utils/netUtil.js')
var util = require('/../../utils/util.js')
var app = getApp();

Page({
  data:{
 		chooseFlag : 1,    //1:显示存茶申请  2:显示提茶申请
 		teaArray : [],   //申请单数组
  },
  onLoad:function(options){
  	
  	var that = this;
  	
  	app.checkHasRegister(null,function() {
  		var from = options.from;
  		if(from == 'saveTea') {
  			that.toSaveTeaOrder();   //如果是来自存茶页面，就默认选中存茶申请单
  		} else if(from == 'getTea') {
  			that.toGetTeaOrder();    //如果是来自提茶页面，就默认选中提茶申请单
  		} else {
  			that.toSaveTeaOrder();   //如果为空，就默认选中提茶申请单
  		}

  	});
  },
  onReady:function(){
  
  },
  onShow:function(){
    
    // 页面显示
  },
  onHide:function(){
    // 页面隐藏
  },

  onUnload:function(){
    // 页面关闭

  },
  
  //点击存茶
  toSaveTeaOrder : function(e) {
  	
  	app.getUserOpedId(this.querySaveTeaArray);
  	
  	this.setData({
  		chooseFlag : 1
  	});
  },
  
  //点击提茶
  toGetTeaOrder : function(e) {
  	
  	app.getUserOpedId(this.queryGetTeaArray);
  	console.log("success")
  	this.setData({
  		chooseFlag : 2
  	});
  },
  
  //查询存茶申请单
	querySaveTeaArray : function() {
		var that = this;
		var params = {
  		method : 'wechat.wisen.in.voucher.query',
  		openId : app.globalData.userInfo.openid
  	};
  	
  	netUtil.buildRequest(that,'/wisen/api/wechat/ret',params,{
  		onPre: function() {
				netUtil.showLoadingDialog(that);
			},
			onSuccess : function(resp) {
			 	netUtil.hideLoadingDialog(that);
				//如果请求成功
				if(resp.ret == app.globalData.respSuccessCode) {
					
					var data = resp.data;
					
					that.setData({
						teaArray : data
					});
					
				} else {
				
						if(resp.message != undefined && resp.message != null && resp.message != '') {
 							netUtil.showAlertDialog("提示", resp.message, false, "确定", null, null);
 						} else {
 							netUtil.showAlertDialog("提示", "网络请求失败", false, "确定", null, null);
 						}
				}				
			},
			onError: function(msgCanShow, code, hiddenMsg) {
        console.log("fail");
				netUtil.hideLoadingDialog(that);
				netUtil.showAlertDialog("提示", msgCanShow, false, "确定", null, null);
			}
  		
  	}).send();
	},
	
	//查询提茶 申请单
	queryGetTeaArray : function() {
		var that = this;
		var params = {
  		method : 'wechat.wisen.out.voucher.query',
  		openId : app.globalData.userInfo.openid
  	};
  	
  	netUtil.buildRequest(that,'/wisen/api/wechat/ret',params,{
  		onPre: function() {
				netUtil.showLoadingDialog(that);
			},
			onSuccess : function(resp) {
			 	
			 	netUtil.hideLoadingDialog(that);
				//如果请求成功
				if(resp.ret == app.globalData.respSuccessCode) {
					
					var data = resp.data;
					
					that.setData({
						teaArray : data
					})
					
				} else {
					netUtil.showAlertDialog("提示", resp.message, false, "确定", null, null);
				}				
			},
			onError: function(msgCanShow, code, hiddenMsg) {
        console.log("fail");
				netUtil.hideLoadingDialog(that);
				netUtil.showAlertDialog("提示", msgCanShow, false, "确定", null, null);
			}
  		
  	}).send();
	},
	
	//跳转到订单详情页面
	toOrderDeatilPage : function(e) {
		var chooseFlag = this.data.chooseFlag;
		var voucherId = e.currentTarget.dataset.voucherid;
		var status = e.currentTarget.dataset.status;
		if(chooseFlag == 1) {    //跳转到 存茶申请单明细页面
				 
			wx.navigateTo({
 				 url: '/pages/save-order-detail/save-order-detail?voucherId=' + voucherId
			})
			
		} else if(chooseFlag == 2) {     //跳转到 提茶申请单明细页面

			wx.navigateTo({
 				 url: '/pages/get-order-detail/get-order-detail?voucherId=' + voucherId
			})
		
		}
	},
	
	  //跳转到存茶 或者提茶 单详情修改页面
  toOrderUpdatePage : function(e) {	
  		var chooseFlag = this.data.chooseFlag;
  		var voucherId = e.currentTarget.dataset.voucherid;
  	  
  	  if(chooseFlag == 1) {  //跳转到 存茶申请单明细修改页面
  	  	wx.navigateTo({
 				 url: '/pages/save-order-update/save-order-update?voucherId=' + voucherId
				})
  	  } else if(chooseFlag == 2) {  //跳转到 提茶申请单明细修改页面
  	  	wx.navigateTo({
 				 url: '/pages/get-order-update/get-order-update?voucherId=' + voucherId
				})
  	  }		
  },
  
  //取消订单的方法
  cancelOrder : function(e) {
  		
  		var that = this;
  		
  		var chooseFlag = that.data.chooseFlag;
  		var voucherId = e.currentTarget.dataset.voucherid;
  		var method = '';
  		if(chooseFlag == 1) {    // 取消存茶申请单
  			method = 'wechat.wisen.in.voucher.cancel';
  		} else if(chooseFlag == 2) {   //取消提茶申请单
  			method = 'wechat.wisen.out.voucher.cancel';
  		}
  		
  		app.getUserOpedId(function() {
  			var params = {
  				method : method,
  				openId : app.globalData.userInfo.openid,
  				data : {
  					voucherId : voucherId
  				}
  			};
  			
  		netUtil.buildRequest(that,'/wisen/api/wechat/ret',params,{
  			onPre: function() {
					netUtil.showLoadingDialog(that);
				},
				onSuccess : function(resp) {
			 	
			 		netUtil.hideLoadingDialog(that);
					//如果请求成功
					if(resp.ret == app.globalData.respSuccessCode) {
						
						//弹出对话框提示订单取消成功
						netUtil.showAlertDialog("提示", '订单取消成功', false, "确定", null, function() {
								//用户点击确认后刷新页面
								wx.reLaunch({
									url: '/pages/order/order',
								});
						});
					
					} else {
						netUtil.showAlertDialog("提示", resp.message, false, "确定", null, null);
					}				
				},
				onError: function(msgCanShow, code, hiddenMsg) {
     	   	console.log("fail");
					netUtil.hideLoadingDialog(that);
					netUtil.showAlertDialog("提示", msgCanShow, false, "确定", null, null);
				}
  		
  		}).send();
  			
  	});
  }
})